﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Avatar.ADService;
using AvatarHelper;
using Avatar.Model;

public partial class Company_CreateOrder : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if (!IsPostBack)
            {
                if (Request.QueryString["adid"] != null)
                {
                    var _id = new Guid(Request.QueryString["adid"].ToString());
                    using (var _adc = DBHelper.GetDataContext())
                    {
                        var _ad = _adc.AD.Where(a => a.ID == _id).FirstOrDefault();
                        if (_ad != null)
                        {
                            lbl_ADCategoryName.Text = _ad.ADCategory.CategoryName;
                            lbl_ADTitle.Text = _ad.Title;
                        }
                        else
                        {
                            throw new Exception("广告内容不存在无法生成订单");
                        }
                    }
                }
                ddl_Period.DataSource = DBHelper.GetAll<Period>("获取价格信息列表出错:");
                ddl_Period.DataBind();
            }
        }
        catch (Exception _e)
        {
            lbl_Error.Text = _e.Message;
        }
    }
    protected void btn_Next_Click(object sender, EventArgs e)
    {
        if (ddl_PaymentList.SelectedValue == "2") Response.Redirect("./Company_BankTransfer.aspx");
        if (ddl_PaymentList.SelectedValue == "3") Response.Redirect("./Company_Remittance.aspx");
        if (ddl_PaymentList.SelectedValue == "1")
        {
            var _ad = ADManager.GetAD(Request.QueryString["adid"] == null ? Guid.Empty : new Guid(Request.QueryString["adid"].ToString()));
            var _adprice = ADPriceManager.GetPrice(_ad.ADCategoryID, Convert.ToInt32(ddl_Period.SelectedValue));

           Guid _orderID= OrderManager.AddNewOrder(_ad.ID, Convert.ToInt32(ddl_Period.SelectedValue), _adprice.ID);


        }

    }
    protected void ddl_Period_SelectedIndexChanged(object sender, EventArgs e)
    {
        var _ad = ADManager.GetAD(Request.QueryString["adid"] == null ? Guid.Empty : new Guid(Request.QueryString["adid"].ToString()));
        var _adprice = ADPriceManager.GetPrice(_ad.ADCategoryID, Convert.ToInt32(ddl_Period.SelectedValue));
        if (_adprice != null)
        {
            lbl_Price.Text = _adprice.Price.ToString();
        }
    }
}
